package com.googlecode.maratische.google.testBrowser;

import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.html.HTMLDocument;


public class ExampleWithWidth extends JFrame {

	public static void main(String[] args) {
		ExampleWithWidth example1 = new ExampleWithWidth();
		example1.setVisible(true);
	}
	
	private JTextPane textPane;
	private JScrollPane jScrollPane;
	
	public ExampleWithWidth() {
		setSize(700, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		
		jScrollPane = new JScrollPane();
		jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		textPane = new JTextPane();
       	textPane.setContentType( "text/html" );
        textPane.setEditable(false);
        textPane.setFocusable(false);
        jScrollPane.setFocusable(true);
		HTMLDocument document = new HTMLDocument();
        		textPane.setStyledDocument(document);
		jScrollPane.setViewportView(textPane);
		add(jScrollPane,BorderLayout.CENTER);

		try {
			
			BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Documents and Settings\\Gizatullinm\\.rssReader\\media\\18\\a.html")));
			StringBuilder sb = new StringBuilder();
			String temp = null;
			while ( (temp = br.readLine()) != null) {
				sb.append(temp);
			}
			String res = sb.toString();
			
//			textPane.setText(res);
			textPane.setText(fixWidthProblem(res));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private String fixWidthProblem(String text) {
		/**
		 * FIXME пробуем решить проблему 66
		 */
		StringBuilder description2 = new StringBuilder();
		String description = text;
		if (description != null) {
			String temp = null;
			Pattern p1 = Pattern.compile(".*[ ](width)[ =].*");
			Pattern p2 = Pattern.compile(".*[ ]height[ =].*");
			while ( (temp = getBetweenString(description, "<img ", ">")) != null) {
				temp = "<img "+temp+">";
				description2.append(getBeforeString(description, "<img "));
				description = getAfterString(description, "<img ", ">");
				int i =  0;
				boolean b1 = false;
				boolean b2 = false;
				if ( b1 = p1.matcher(temp).matches()) {
					i++;
				}
				if (b2 = p2.matcher(temp).matches()) {
					i++;
				}
				if (i==1) {
					//проблема!
					if (b1) {
//						logger.debug("remove width from "+temp);
						temp = temp.replaceFirst(" width", " ");
					}
					if (b2) {
//						logger.debug("remove height from "+temp);
						temp = temp.replaceFirst(" height", " ");
					}
				}
				description2.append(temp);
			}
			description2.append(description);
		}
		return description2.toString();
	}
	

	public static String replace(String text, String oldSub, String newSub) {
		while (text.indexOf(oldSub)>-1) {
			text = text.substring(0, text.indexOf(oldSub))+newSub+text.substring(text.indexOf(oldSub)+oldSub.length());
		}
		return text;
	}
	
	public static String getBetweenString(String text, String start, String end) {
		int i = 0;
		int j = 0;
		if ((i = text.indexOf(start)) > -1 && (j = text.indexOf(end, i + start.length())) > -1) {
			return text.substring(i + start.length(), j);
		}
		return null;
	}
	
	static public String removeXml(String str) {
		int sz = str.length();
		StringBuffer buffer = new StringBuffer(sz);
		boolean inTag = false;
		for (int i = 0; i < sz; i++) {
			char ch = str.charAt(i);
			if (ch == '<') {
				inTag = true;
			} else if (ch == '>') {
				inTag = false;
				continue;
			}
			if (!inTag) {
				buffer.append(ch);
			}
		}
		return buffer.toString();
	}

	public static String getAfterString(String text, String start, String end) {
		int i = 0;
		int j = 0;
		if ((i = text.indexOf(start)) > -1 && (j = text.indexOf(end, i + start.length())) > -1) {
			return text.substring(j + end.length());
		}
		return null;
	}

	public static String getBeforeString(String text, String start) {
		if (text.indexOf(start) > -1) {
			return text.substring(0, text.indexOf(start));
		}
		return text;
	}

}
